var iDisplayStart=0,iDisplayLength=20,totalPageNumAll,totalPageNumD,totalPageNumA,pageNumAll=0,pageNumD=0,pageNumA=0,AllSearchValue='',DSearchValue='',ASearchValue='';
var partSelectId="",partSelectText="";
$(function(){
	//ajax全局设置
	$.ajaxSetup({
		timeout:30000,
		beforeSend: function(request) {
            request.setRequestHeader("X-AIRPORT-TOKEN", localStorage.getItem("oauth_token"));
        },
		error:function(xhr,status){
			layer.msg("网络连接已超时，请重新刷新！");
		}
	})
	//保存数据
	$('#specialTemDataSave').on('click',function(e){
		var specialPartSelectedItem=$('#specialPartSelectedItem').find('li');
		var specialPresentSelectedItem=$('#specialPresentSelectedItem').find('li');
		var specialAirportItem=$("#specialAirportItem").find('li');
		var specialAirportSelectedItem=$("#specialAirportItem>div").find('li.airportSelectBK');
		var specialAirportInput=$("#specialAirport").val();
		var specialDepartInput=$("#specialDepart").val();
		var specialPresentInput=$("#specialPresent").val();
		var specialRank=$('#specialRank').find('span.specialLevelSeleted');
		if($("#specialTemName").val()==""){
			layer.msg('特情标题不能为空')
			return;
		}
		if($("#specialTemName").val().length>100){
			layer.msg('特情标题字数不要超过100!')
			return;
		}
		if($("#specialTemCon").val()==""){
			layer.msg('特情内容不能为空')
			return;
		}
		if(getByteLen($("#specialTemCon").val()).length>1024){
			layer.msg('特情内容字数不要超过1024字符!')
			return;
		}
		if(specialPartSelectedItem.length==0){
			layer.msg('请关联部门');
			return;
		}
		if(specialDepartInput==""){
			layer.msg('请确定选择好的关联部门');
			return;
		}
		if(specialRank.length==0){
			layer.msg('请选择特情等级');
			return;
		}
		/*if(specialAirportInput!=""){
			var reg = /^([0-9a-zA-Z]+[,]?)*$/;
			if(!reg.test(specialAirportInput)){
				layer.msg("您输入的关联的航班信息不符合规范！请按照正确的格式输入，如：RNG8888,EDG8888")
				return;
			}
		}*/
		var template={"id":null,"topic":$("#specialTemName").val(),"content":$("#specialTemCon").val(),"level":$('#specialRank').find('span.specialLevelSeleted').text()};
		var itemDeptList=[];
		var itemUserList=[];
		var itemFlightList=[];
		if(specialPartSelectedItem.length!=0){
			$.each(specialPartSelectedItem,function(i,n){
				itemDeptList.push({"id":null,"templateId":null,"subCode":$(n).attr('id'),"subName":$(n).text(),"subType":"1"});
			})
		}
		if(specialPresentSelectedItem.length!=0 && specialPresentInput!=""){
			$.each(specialPresentSelectedItem,function(i,n){
				itemUserList.push({"id":null,"templateId":null,"subCode":$(n).attr('id'),"subName":$(n).text(),"subType":"2"});
			})
		}
		if(specialAirportInput!=""){
			var specialAirportInputArray=specialAirportInput.split(",")
			$.each(specialAirportInputArray,function(i,n){
					itemFlightList.push({"id":null,"templateId":null,"subCode":null,"subName":n,"subType":"3"});
			})
			/*if(specialAirportSelectedItem.length!=0){
				$.each(specialAirportInputArray,function(i,n){
					$.each(specialAirportSelectedItem,function(a,b){
						if(n==$(b).text()){
							itemFlightList.push({"id":null,"templateId":null,"subCode":$(b).attr('id'),"subName":$(b).text(),"subType":"3"});
							delete specialAirportInputArray[i];
						}
					})
				})
				if(specialAirportInputArray!="[]" && specialAirportInputArray.length!=0){
					$.each(specialAirportInputArray,function(i,n){
						if(n!=undefined && n!="undefined"){
							itemFlightList.push({"id":null,"templateId":null,"subCode":null,"subName":n,"subType":"3"});
						}
					})
				}
			}else{
				$.each(specialAirportInputArray,function(i,n){
					itemFlightList.push({"id":null,"templateId":null,"subCode":null,"subName":n,"subType":"3"});
				})
			}*/
		}
		var templetData={"template":template,"itemDeptList":itemDeptList,"itemUserList":itemUserList,"itemFlightList":itemFlightList,"loginName":localStorage.oauth_token};
		layer.confirm('您确认发布？',function(index){
			layer.close(index);
			var loadIndex=layer.load("数据正在努力提交中，请稍等!");
			setTimeout(function(){
				$.ajax({
					type:'POST',
					url:baseURL+'/api/special/publish',
					dataType:'json',
					contentType:"application/json",
					data:JSON.stringify(templetData),
					success:function(result){
						layer.close(loadIndex);
						if(!result.flag){
							layer.msg("数据错误:"+result.message)
						}else{
							layer.msg('发布成功',1,1);
							$("#specialAddForm").find("input,textarea,select").val("");
							window.location.href=baseURL+"/api/special/page?ouath_token="+localStorage.getItem("oauth_token");
						}
					}
					})
			},2000)
		})
	})
	
	var specialAddPart=function(){$.ajax({//部门
		type:'GET',
		url:baseURL+'/services/dept/listSSRDept',
		dataType:'json',
		success:function(result){
			if(result.errorcode=="1"){
        		  layer.msg("查询失败！");
        		  return;
			}
			if(result.errorcode=="0"){
				if(result.results==null){
					layer.msg('没有数据')
					return false;
				}
			var htmlPart="";
			var obj=result.results;
			function returnBack(obj){
				if(!obj) return;
				$.each(obj,function(i,n){
						if(this.subDepartmentList!=null && this.subDepartmentList!="[]" && this.subDepartmentList.length!=0){
							if(this.includeRole){
								htmlPart+="<li><span class='folder' id='"+this.id+"' code='"+this.code+"'>"+this.name+"</span><ul>";
							}else htmlPart+="<li><span class='folder disabled c-999' id='"+this.id+"' code='"+this.code+"'>"+this.name+"</span><ul>";
							returnBack(this.subDepartmentList)
							htmlPart+="</ul>";
						}else {
							if(this.includeRole){
								htmlPart+="<li><span class='file' id='"+this.id+"' code='"+this.code+"'>"+this.name+"</span>";	
							}else htmlPart+="<li><span class='file disabled c-999' id='"+this.id+"' code='"+this.code+"'>"+this.name+"</span>";	
						}
				})
				htmlPart+="</li>";
			}
			returnBack(obj)
			$('#specialPartItem').append(htmlPart)
			$('#specialPartItem').find('span:not(.disabled)').on('click',function(e){
				e.stopPropagation();
				$('#specialPartItem').find('span').removeClass('commonSelectBK')
				$(e.target).addClass('commonSelectBK');
				partSelectId=$(e.target).attr('id');
				partSelectText=$(e.target).text();
			})
			$('#specialPartSelectedItemAddBtn').on('click',function(e){
				e.stopPropagation()
				if(partSelectId=="" && partSelectText==""){
					layer.msg("请选择部门！")
					return false;
				}
				var specialPartSelectedItem=$('#specialPartSelectedItem').find('li');
				var flag=true;
				if(specialPartSelectedItem.length!=0){
					$.each(specialPartSelectedItem,function(i,n){
						if($(n).attr('id')==partSelectId){
							layer.msg('请不要重复添加')
							flag=false;
						}
					})
					if(flag){
						$('#specialPartSelectedItem').append("<li id='"+partSelectId+"' onclick=\"changePortSelect(this)\">"+partSelectText+"</li>");
					}
				}else {
					$('#specialPartSelectedItem').append("<li id='"+partSelectId+"' onclick=\"changePortSelect(this)\">"+partSelectText+"</li>");
				}
			})
			$("#specialPartSelectedItemShowBtn").on('click',function(e){
				$(e.target).parents("div.specialSelect").hide();
				inputPartHtml="";
				$.each($('#specialPartSelectedItem').find('li'),function(i,n){
					inputPartHtml+=$(n).text()+",";
				})
				$("#specialDepart").val(specialSubString(inputPartHtml))
			})
			$('#specialPartSelectedItemRemoveBtn').on('click',function(e){
					inputPartHtml="";
					e.stopPropagation();
					var specialPartRemoveItem=$('#specialPartSelectedItem').find('li.airportSelectBK');
					if(specialPartRemoveItem.length==0){
						return false;
					}
					if(specialPartRemoveItem.length!=0){
						$.each(specialPartRemoveItem,function(i,n){
							$(n).remove();
						})
					}
					$.each($('#specialPartSelectedItem').find('li'),function(i,n){
						inputPartHtml+=$(n).text()+",";
					})
					$("#specialDepart").val(specialSubString(inputPartHtml))
			})
			$("#specialPartSelectedItemHiddenBtn").on('click',function(){
				$("#specialDepart").val("")
				$('#specialPartSelectedItem').empty();
			})
			$("#specialPartItem").treeview({
				animated: "fast",
				collapsed: true,
				unique: true,
				//persist: "cookie",
				toggle: function() {
					//window.console && console.log("%o was toggled", this);
				}
			})
		}
		},
		error:function(e){
			layer.msg(e)
		}
	})
	}
	specialAddPart();
	//抄送人员
	var specialAddPresent=function(){$.ajax({
		type:'GET',
		url:baseURL+'/services/dept/listDept',
		dataType:'json',
		success:function(result){
			if(result.errorcode=="1"){
        		  layer.msg("查询失败！");
        		  return;
			}
			if(result.errorcode=="0"){
				if(result.results==null){
					layer.msg('没有数据')
					return false;
				}
			var htmlPart="";
			var obj=result.results;
			function returnBack(obj){
				if(!obj) return;
				$.each(obj,function(){
						if(this.subDepartmentList!=null && this.subDepartmentList!="[]" && this.subDepartmentList.length!=0){
							htmlPart+="<li><span class='folder' id='"+this.id+"' code='"+this.code+"'>"+this.name+"</span><ul>";
							returnBack(this.subDepartmentList)
							htmlPart+="</ul>";
						}else {
							htmlPart+="<li><span class='file' id='"+this.id+"' code='"+this.code+"'>"+this.name+"</span>";	
						}
				})
				htmlPart+="</li>";
			}
			returnBack(obj)
			$('#specialPresentItem').append(htmlPart)
			$('#specialPresentItem').find('span').on('click',function(e){
				e.stopPropagation();
				$('#specialPresentItem').find('span').removeClass('commonSelectBK')
				$(e.target).addClass('commonSelectBK')
				$('#specialPresentSelectedAllItem').empty();
				specialPresent($(e.target).attr('id'));
			})
			$("#specialPresentSelectedItemShowBtn").on('click',function(e){
				$(e.target).parents("div.specialSelect").hide();
				inputPresentHtml="";
				if($('#specialPresentSelectedItem').find('li').length!=0){
					$.each($('#specialPresentSelectedItem').find('li'),function(i,n){
						inputPresentHtml+=$(n).text()+",";
					})
					$("#specialPresent").val(specialSubString(inputPresentHtml))
				}else $("#specialPresent").val("");
				$("#specialPresentSelectedAllItem").find('li').removeClass("airportSelectBK");
			})
			$('#specialPresentSelectedItemAddBtn').on('click',function(e){
					e.stopPropagation()
					var specialPresentSelectedItem=$('#specialPresentSelectedItem').find('li');
					var specialPresentSelectedAllItem=$('#specialPresentSelectedAllItem').find('li.airportSelectBK');
					if(specialPresentSelectedAllItem.length==0){
						layer.msg("请选择人员！")
						return false;
					}
					var flag=true;
					if(specialPresentSelectedItem.length!=0){
						$.each(specialPresentSelectedAllItem,function(i,n){
							$.each(specialPresentSelectedItem,function(o,p){
								if($(n).attr('id')==$(p).attr('id')){
									layer.msg('请不要重复添加');
									flag=false;
								}
							})
						})
						if(flag){
							$.each(specialPresentSelectedAllItem,function(i,n){
								$('#specialPresentSelectedItem').append("<li id='"+$(n).attr('id')+"' onclick=\"changePortSelect(this)\">"+$(n).text()+"</li>");
							})
						}
					}else {
						$.each(specialPresentSelectedAllItem,function(i,n){
							$('#specialPresentSelectedItem').append("<li id='"+$(n).attr('id')+"' onclick=\"changePortSelect(this)\">"+$(n).text()+"</li>");
						})
					}
			})
			$('#specialPresentSelectedItemRemoveBtn').on('click',function(e){
						inputPresentHtml="";
						e.stopPropagation();
						var specialPartRemoveItem=$('#specialPresentSelectedItem').find('li.airportSelectBK');
						var specialPartRemoveAllItem=$('#specialPresentSelectedAllItem').find('li');
						$.each(specialPartRemoveAllItem,function(i,n){
							$.each(specialPartRemoveItem,function(o,p){
								if($(n).attr('id')==$(p).attr('id')){
									$(n).removeClass("airportSelectBK");
								}
							})
						})
						if(specialPartRemoveItem.length==0){
							return false;
						}
						if(specialPartRemoveItem.length!=0){
							$.each(specialPartRemoveItem,function(i,n){
								$(n).remove();
							})
						}
			})
			$("#specialPresentSelectedItemHiddenBtn").on('click',function(){
				$("#specialPresent").val("");
				$('#specialPresentSelectedItem').empty();
				$('#specialPresentSelectedAllItem').find('li').removeClass("airportSelectBK");
				
			})
			$("#specialPresentItem").treeview({
				animated: "fast",
				collapsed: true,
				unique: true,
				//persist: "cookie",
				toggle: function() {
					//window.console && console.log("%o was toggled", this);
				}
			})
		}
		},
		error:function(e){
			layer.msg(e)
		}
	})
	}
	specialAddPresent();
	//相关角色
	/*var specialPart=function(id,partName){
		$.ajax({    
		type:'GET',
		url:baseURL+'/services/dept/listRole/'+id,
		dataType:'json',
		contentType:"application/json",
		success:function(result){
			if(result.errorcode=="1"){
          		  layer.msg("查询失败！");
          		  return false;
			}
			if(result.errorcode=="0"){
				if(result.results==null){
					layer.msg('没有数据')
					return false;
				}
				var html="";
				var inputPartHtml_1,inputPartHtml;
				if(result.results!=null && result.results.length!=0 && result.results!="[]"){
					$.each(result.results,function(i,n){
						html+="<li roleCode='"+n.code+"'>"+n.name+"<span class='ml-5 c-red'>("+partName+")</span>"+"</li>"
					})
					$('#specialPartSelectedAllItem').html(html);
				}else $('#specialPartSelectedAllItem').html("<div class='text-c mt-20'>无数据</div>");
				$('#specialPartSelectedAllItem').find('li').on('click',function(e){
					inputPartHtml="";
					e.stopPropagation();
					$(e.target).toggleClass("commonSelectColor");
					var specialPartSelectedItem=$('#specialPartSelectedAllItem').next().find('li');
					var flag=true;
					if(specialPartSelectedItem.length!=0){
						$.each(specialPartSelectedItem,function(i,n){
							if($(n).attr('roleCode')==$(e.currentTarget).attr('roleCode')){
								layer.msg('请不要重复添加')
								flag=false;
							}
						})
						if(flag){
							$('#specialPartSelectedAllItem').next().append("<li subCode='"+id+"' subName='"+partName+"' roleCode ='"+$(e.currentTarget).attr('roleCode')+"'>"+$(e.currentTarget).html()+"<span class='f-r mr-10 iconDelete'>x</span></li>");
						}
					}else {
						$('#specialPartSelectedAllItem').next().append("<li subCode='"+id+"' subName='"+partName+"' roleCode='"+$(e.currentTarget).attr('roleCode')+"'>"+$(e.currentTarget).html()+"<span class='f-r mr-10 iconDelete'>x</span></li>");
					}
					$.each($('#specialPartSelectedItem').find('li'),function(i,n){
						inputPartHtml+=specialSubString($(n).text())+",";
					})
					$("#specialDepart").val(specialSubString(inputPartHtml))
					$('.iconDelete').on('click',function(ev){
						ev.stopPropagation();
						inputPartHtml_1="";
						$(ev.target).parent().remove();
						$.each($("#specialPartSelectedItem").find('li'),function(i,n){
							inputPartHtml_1+=specialSubString($(n).text())+",";
						})
						$("#specialDepart").val(specialSubString(inputPartHtml_1));
					})
				})
			}
		},
		error:function(e){
			layer.msg(e)
		}
	})
}*/
	//相关人员
	var specialPresent=function(id){
		$.ajax({    
		type:'GET',
		url:baseURL+'/services/dept/listEmployee/'+id,
		dataType:'json',
		contentType:"application/json",
		success:function(result){
			if(result.errorcode=="1"){
          		  layer.msg("查询失败！");
          		  return false;
			}
			if(result.errorcode=="0"){
				if(result.results==null){
					layer.msg('没有数据')
					return false;
				}
				var html="";
				var inputPresentHtml,inputPresentHtml_1;
				if(result.results!=null && result.results.length!=0 && result.results!="[]"){
					$.each(result.results,function(i,n){
						html+="<li id='"+n.id+"' onclick=\"changePortSelect(this)\">"+n.empName+"</li>"
					})
					$('#specialPresentSelectedAllItem').html(html);
				}else $('#specialPresentSelectedAllItem').html("<div class='text-c mt-20'>无数据</div>");
			}
		},
		error:function(e){
			layer.msg(e)
		}
	})
}
	
	
	//航班
	var specialAirport=function(type,page,obj){
		$("#specialAirportLoad").show();
		$.ajax({   
		type:'GET',
		url:baseURL+'/api/special/template/listFlight?type='+type+'&flightNo='+obj+'&iDisplayStart='+iDisplayLength*page+'&iDisplayLength='+iDisplayLength,
		dataType:'json',
		success:function(result){
			$("#specialAirportLoad").hide();
			var fligthAll="";
			if(result.results==null){
				$("#airportTypeAllList").empty().html("<li class='disabled'>没有数据</li>");
			}else{
				totalPageNumAll=Math.floor(result.totalcount/iDisplayLength);
				$.each(result.results,function(i,n){
					fligthAll+="<li class='text-c airportBK radius' id='"+n.flightId+"'>"+n.flightNo+"</li>";
				})
				$("#airportTypeAllList").html(fligthAll);
				$('#airportTypeAllList').find('li').on('click',function(e){
					e.stopPropagation();
					//$(e.target).toggleClass("airportSelectBK");
					var fligthInputHtml="",specialAirportInputArray=[],specialAirportInput="";
					$(e.target).toggleClass("airportSelectBK");
					specialAirportInput=$("#specialAirport").val();
					if($(e.target).hasClass("airportSelectBK")){
						if(specialAirportInput.indexOf($(e.target).text())!=-1){
							layer.msg('请不要重复添加！')
						}else{
							if(specialAirportInput!=""){
								specialAirportInput+=","+$(e.target).text();
							}else specialAirportInput+=$(e.target).text();
							$("#specialAirport").val(specialAirportInput);
						}
					}else{
						specialAirportInputArray=specialAirportInput.split(",")
						$.each(specialAirportInputArray,function(i,n){
							if(n==$(e.target).text()){
								delete specialAirportInputArray[i]
							}
						})
						if(specialAirportInputArray!="[]" && specialAirportInputArray.length!=0){
							$.each(specialAirportInputArray,function(i,n){
								if(n!=undefined && n!="undefined"){
									fligthInputHtml+=n+",";
								}
							})
						}
						$("#specialAirport").val(specialSubString(fligthInputHtml));
					}
				})
				$('#specialAirportSelectedItemShowBtn').on('click',function(e){
					$(e.target).parents("div.specialSelect").hide();
				})
				$("#specialAirportSelectedItemHiddenBtn").on('click',function(){
					$("#specialAirport").val("");
					$('#airportTypeAllList').find('li').removeClass("airportSelectBK");
				})
			}
			
		},
		error:function(e){
			layer.msg(e)
		}
	})
	}
	specialAirport("All",0,"");
	$('.nextPagitionAll').on('click',function(e){
		e.stopPropagation();
		++pageNumAll;
		if(pageNumAll>totalPageNumAll){
			pageNumAll=totalPageNumAll
			return;
		}else{
			if($(".searchTemplet").val()!=""){
				specialAirport("All",pageNumAll,$(".searchTemplet").val());
			}else specialAirport("All",pageNumAll,'');
		}
	})
	$('.prevPagitionAll').on('click',function(e){
		e.stopPropagation();
		--pageNumAll;
		if(pageNumAll<=-1){
			pageNumAll=0;
			return;
		}else{
			if($(".searchTemplet").val()!=""){
				specialAirport("All",pageNumAll,$(".searchTemplet").val());
			}else specialAirport("All",pageNumAll,'');
		}
	})
/*	$('.searchTemplet').on('keyup',function(e){
		$.each($('#specialAirportItem').find('li.airportSelectBK'),function(i,n){
			if($(e.target).val().indexOf($(n).text())==-1){
				$(n).removeClass('airportSelectBK');
			}
		})
		specialAirport("All",0,$(".searchTemplet").val());
	})*/
	$('.doSearch').on('click',function(e){
		specialAirport("All",0,$(".searchTemplet").val());
	})
	/*$.Huitab("#specialAirportItem .tabBar span","#specialAirportItem .tabCon","current","click","0");
	var specialAirport=function(type,page,obj){
		$.ajax({   
		type:'GET',
		url:baseURL+'/api/special/template/listFlight?type='+type+'&iDisplayStart='+iDisplayLength*page+'&iDisplayLength='+iDisplayLength,
		dataType:'json',
		success:function(result){
			    var fligthAll="",importAll="",exportAll="";
				if(type=="All"){
					if(result.results.length==0){
						$("#airportTypeAllList").empty().html("<li class='disabled'>没有数据</li>");
					}else{
						totalPageNumAll=Math.floor(result.totalcount/iDisplayLength);
						$.each(result.results,function(i,n){
							fligthAll+="<li class='text-c airportBK radius' id='"+n.flightId+"'>"+n.flightNo+"</li>";
						})
						$("#airportTypeAllList").html(fligthAll);
						$('#airportTypeAllList').find('li').on('click',function(e){
							e.stopPropagation();
							var fligthInputHtml="",specialAirportInputArray=[],specialAirportInput="";
							$(e.target).toggleClass("airportSelectBK");
							specialAirportInput=$("#specialAirport").val();
							if($(e.target).hasClass("airportSelectBK")){
								if(specialAirportInput.indexOf($(e.target).text())!=-1){
									layer.msg('请不要重复添加！')
								}else{
									if(specialAirportInput!=""){
										specialAirportInput+=","+$(e.target).text();
									}else specialAirportInput+=$(e.target).text();
									$("#specialAirport").val(specialAirportInput);
								}
							}else{
								specialAirportInputArray=specialAirportInput.split(",")
								$.each(specialAirportInputArray,function(i,n){
									if(n==$(e.target).text()){
										delete specialAirportInputArray[i]
									}
								})
								if(specialAirportInputArray!="[]" && specialAirportInputArray.length!=0){
									$.each(specialAirportInputArray,function(i,n){
										if(n!=undefined && n!="undefined"){
											fligthInputHtml+=n+",";
										}
									})
								}
								$("#specialAirport").val(specialSubString(fligthInputHtml));
							}
						})
					}
				}else if(type=="D"){
					if(result.results.length==0){
						$("#airportTypeDList").empty().html("<li class='disabled'>没有数据</li>");
					}else{
						totalPageNumD=Math.floor(result.totalcount/iDisplayLength);
						$.each(result.results,function(i,n){
							importAll+="<li class='text-c airportBK radius' id='"+n.flightId+"'>"+n.flightNo+"</li>";
						})
						$("#airportTypeDList").html(importAll);
						$('#airportTypeDList').find('li').on('click',function(e){
							e.stopPropagation();
							var fligthInputHtml="",specialAirportInputArray=[],specialAirportInput="";
							$(e.target).toggleClass("airportSelectBK");
							specialAirportInput=$("#specialAirport").val();
							if($(e.target).hasClass("airportSelectBK")){
								if(specialAirportInput.indexOf($(e.target).text())!=-1){
									layer.msg('请不要重复添加！')
								}else{
									if(specialAirportInput!=""){
										specialAirportInput+=","+$(e.target).text();
									}else specialAirportInput+=$(e.target).text();
									$("#specialAirport").val(specialAirportInput);
								}
							}else{
								specialAirportInputArray=specialAirportInput.split(",")
								$.each(specialAirportInputArray,function(i,n){
									if(n==$(e.target).text()){
										delete specialAirportInputArray[i]
									}
								})
								if(specialAirportInputArray!="[]" && specialAirportInputArray.length!=0){
									$.each(specialAirportInputArray,function(i,n){
										if(n!=undefined && n!="undefined"){
											fligthInputHtml+=n+",";
										}
									})
								}
								$("#specialAirport").val(specialSubString(fligthInputHtml));
							}
						})
					}
				}else if(type=="A"){
					if(result.results.length==0){
						$("#airportTypeAList").empty().html("<li class='disabled'>没有数据</li>");
					}else{
						totalPageNumA=Math.floor(result.totalcount/iDisplayLength);
						$.each(result.results,function(i,n){
							exportAll+="<li class='text-c airportBK radius' id='"+n.flightId+"'>"+n.flightNo+"</li>";
						})
						$("#airportTypeAList").html(exportAll);
						$('#airportTypeAList').find('li').on('click',function(e){
							e.stopPropagation();
							var fligthInputHtml="",specialAirportInputArray=[],specialAirportInput="";
							$(e.target).toggleClass("airportSelectBK");
							specialAirportInput=$("#specialAirport").val();
							if($(e.target).hasClass("airportSelectBK")){
								if(specialAirportInput.indexOf($(e.target).text())!=-1){
									layer.msg('请不要重复添加！');
								}else{
									if(specialAirportInput!=""){
										specialAirportInput+=","+$(e.target).text();
									}else specialAirportInput+=$(e.target).text();
									$("#specialAirport").val(specialAirportInput);
								}
							}else{
								specialAirportInputArray=specialAirportInput.split(",")
								$.each(specialAirportInputArray,function(i,n){
									if(n==$(e.target).text()){
										delete specialAirportInputArray[i]
									}
								})
								if(specialAirportInputArray!="[]" && specialAirportInputArray.length!=0){
									$.each(specialAirportInputArray,function(i,n){
										if(n!=undefined && n!="undefined"){
											fligthInputHtml+=n+",";
										}
									})
								}
								$("#specialAirport").val(specialSubString(fligthInputHtml));
							}
						})
					}
				}
		},
		error:function(e){
			layer.msg(e)
		}
	})
	}
	specialAirport("All",0,"");
	specialAirport("D",0,"");
	specialAirport("A",0,"");
	//全部航班
	$('.nextPagitionAll').on('click',function(e){
		e.stopPropagation();
		++pageNumAll;
		if(pageNumAll>totalPageNumAll){
			pageNumAll=totalPageNumAll
			return;
		}else{
			specialAirport("All",pageNumAll,'');
		}
	})
	$('.prevPagitionAll').on('click',function(e){
		e.stopPropagation();
		--pageNumAll;
		if(pageNumAll<=-1){
			pageNumAll=0;
			return;
		}else{
			specialAirport("All",pageNumAll,'');
		}
	})
	$('#airportTypeAllBtn').on('click',function(e){
		AllSearchValue=$(e.target).prev().val();
		if(AllSearchValue!=""){
			specialAirport("All",pageNumAll,AllSearchValue);
		}else specialAirport("All",pageNumAll,'')
		
	})
	//出港航班
	$('.nextPagitionD').on('click',function(e){
		e.stopPropagation();
		++pageNumD;
		if(pageNumD>totalPageNumD){
			pageNumD=totalPageNumD;
			return;
		}else{
			specialAirport("D",pageNumD,'');
		}
	})
	$('.prevPagitionD').on('click',function(e){
		e.stopPropagation();
		--pageNumD;
		if(pageNumD<=-1){
			pageNumD=0;
			return;
		}else{
			specialAirport("D",pageNumD,'');
		}
	})
	$('#airportTypeDBtn').on('click',function(e){
		AllSearchValue=$(e.target).prev().val();
		if(AllSearchValue!=""){
			specialAirport("All",pageNumAll,AllSearchValue);
		}else specialAirport("All",pageNumAll,'')
		
	})
	//进港航班
	$('.nextPagitionA').on('click',function(e){
		e.stopPropagation();
		++pageNumA;
		if(pageNumA>totalPageNumA){
			pageNumA=totalPageNumA
			return;
		}else{
			specialAirport("A",pageNumA,'');
		}
	})
	$('.prevPagitionAll').on('click',function(e){
		e.stopPropagation();
		--pageNumA;
		if(pageNumA<=-1){
			pageNumA=0;
			return;
		}else{
			specialAirport("A",pageNumA,'');
		}
	})
	$('#airportTypeABtn').on('click',function(e){
		AllSearchValue=$(e.target).prev().val();
		if(AllSearchValue!=""){
			specialAirport("All",pageNumAll,AllSearchValue);
		}else specialAirport("All",pageNumAll,'')
		
	})
	$('#specialAirport').on('keyup',function(e){
		$.each($('#specialAirportItem').find('li.airportSelectBK'),function(i,n){
			if($(e.target).val().indexOf($(n).text())==-1){
				$(n).removeClass('airportSelectBK');
			}
		})
	})*/
	
	
	$.ajax({   //特情等级
		type:'GET',
		url:baseURL+'/api/special/template/listLevel',
		dataType:'json',
		success:function(result){
			if(!result.flag){
				layer.msg('数据出错')
			}else{
				var html="";
				$.each(result.results,function(i,n){
					if(n.dictItemName=="T1"){
						html+="<span class='specialLevel_f' style='background:#29c6ef' name='"+n.dictItemName+"'>"+n.dictItemName+"</span>";
					}else if(n.dictItemName=="T2"){
						html+="<span class=' ' style='background:#4784c3' name='"+n.dictItemName+"'>"+n.dictItemName+"</span>";
					}else if(n.dictItemName=="T3"){
						html+="<span class=' ' style='background:#ffc001' name='"+n.dictItemName+"'>"+n.dictItemName+"</span>";
					}else if(n.dictItemName=="T4"){
						html+="<span class=' ' style='background:#ff6600' name='"+n.dictItemName+"'>"+n.dictItemName+"</span>";
					}else if(n.dictItemName=="T5"){
						html+="<span class='specialLevel_l' style='background:#da0000' name='"+n.dictItemName+"'>"+n.dictItemName+"</span>";
					}
				})
				$('#specialRank').append(html);
				$("#specialRank").find('span').on('click',function(e){
					e.stopPropagation();
					$("#specialRank").find('span').removeClass("specialLevelSeleted");
					$(e.target).addClass("specialLevelSeleted");
				})
			}
		},
		error:function(e){
			layer.msg(e)
		}
	})
	
	function specialSubString(args){
		return args.substring(0,args.length-1);
	}
	
	var isMouse=true;
	$('.specialSelectInput').on('click',function(e){
		e.stopPropagation();
		$(e.target).attr('flag','false')
		$.each($('.specialSelectInput'),function(i,n){
			if($(n).attr('flag')=="true"){
				$(n).next().hide();
			}else $(n).attr('flag','true')
		})
		$(e.target).next().toggle();
		$(e.target).next().on('mouseleave',function(ev){
			ev.stopPropagation();
			$(e.target).next().hide();
			isMouse=true;
		})
		$(e.target).next().on('mouseover',function(ev){
			isMouse=false;
		})
	})
	$('.specialSelectInput').on('blur',function(e){
		if(isMouse){
			$(e.target).next().hide();
		}
	})
	
	//模板选择下拉菜单
		$.ajax({   //按钮选择模板
			type:'GET',
			url:baseURL+'/api/special/template/listAll?referer='+Math.random(),
			dataType:'json',
			success:function(result){
				if(!result.flag){
					layer.msg('数据出错')
				}else{
					var dropDownFlag=false;
					$.each(result.results,function(i,n){
						$('.specialDropDownMenu').append("<li id='"+n.id+"'>"+n.topic+"</li>")
					})
					if(result.results.length<10){
						dropDownFlag=false;
					}else dropDownFlag=true;
					$('.specialDropDown').find('button').on('click',function(){
						$('.specialDropDown').find('ul').toggle();
						if($('.specialDropDown').find('ul').is(":hidden")){
							$('#specialDropDownMore').hide();
						}else{
							if(dropDownFlag){
								$('#specialDropDownMore').show();
							}else $('#specialDropDownMore').hide();
						}
					})
					$("#specialDropDownMore").on('click',function(){
						window.location.href=baseURL+"/api/special/pageTemplet/list?ouath_token="+localStorage.getItem("oauth_token")+"&referer="+Math.random();
					})
					$('.specialDropDownMenu').find('li').on('click',function(e){
						e.stopPropagation();
						layer.confirm("您确定运用特情模板："+$(e.target).text(),function(){
							specialTempShow($(e.target).attr('id'));
							layer.msg("成功运用",1,1);
							//$('.specialClose').trigger('click');
							$("#specialPartSelectedItem").empty();
							$("#specialPresentSelectedItem").empty();
							$("#specialDepart").empty();
							$("#specialPresent").empty();
						})
					})
					$(".specialDropDownMenu").find("li:last").css("border","none");
				}
			},
			error:function(e){
				layer.msg(e)
			}
		})
		
		var specialTempShow=function(specialTemId){
		//初始化数据
		$.ajax({
			type:'GET',
			url:baseURL+'/api/special/template/get/'+specialTemId+'?referer='+Math.random(),
			dataType:'json',
			success:function(result){
				if(!result.flag){
					layer.msg("错误："+result.message);
				}
				if(result.flag){
					var results=result.result;
					if(results==null){
						return false
					}
					$("#specialTemName").val(results.topic)
					specialTemId=results.id;
					$("#specialTemCon").val(results.content)
					$("#specialRank").find("span[name='"+results.level+"']").addClass("specialLevelSeleted");
					if(results.itemDeptList!=null){
						var specialPartTempHtml="";
						$.each(results.itemDeptList,function(i,n){
							$("#specialPartSelectedItem").append("<li id='"+n.subCode+"' onclick=\"changePortSelect(this)\">"+n.subName+"</li>")
							specialPartTempHtml+=n.subName+",";
						})
						$("#specialDepart").val(specialSubString(specialPartTempHtml));
						/*$('.iconDelete').on('click',function(ev){
							ev.stopPropagation();
							var inputPartHtml_1="";
							$(ev.target).parent().remove();
							$.each($("#specialPartSelectedItem").find('li'),function(i,n){
								inputPartHtml_1+=$(n).text()+",";
							})
							$("#specialDepart").val(specialSubString(inputPartHtml_1));
						})*/
					}
					if(results.itemUserList!=null){
						var specialPresentTempHtml="";
						$.each(results.itemUserList,function(i,n){
							$("#specialPresentSelectedItem").append("<li id='"+n.subCode+"' onclick=\"changePortSelect(this)\">"+n.subName+"</li>")
							specialPresentTempHtml+=n.subName+",";
						})
						$("#specialPresent").val(specialSubString(specialPresentTempHtml));
						/*$('.iconDelete').on('click',function(ev){
							ev.stopPropagation();
							var inputPresentHtml_1="";
							$(ev.target).parent().remove();
							$.each($("#specialPresentSelectedItem").find('li'),function(i,n){
								inputPresentHtml_1+=$(n).text()+",";
							})
							$("#specialPresent").val(specialSubString(inputPresentHtml_1));
						})*/
					}
					var itemFligthListIs=results.itemFlightList;
					var itemFligthListHtml="";
					$("#specialAirportItem").find("li").removeClass("airportSelectBK");
					if(itemFligthListIs!=null){
						/*$.each($("#specialAirportItem").find("li"),function(i,n){
							$.each(results.itemFlightList,function(a,b){
								if(b.subCode==$(n).attr('id')){
									$(n).addClass("airportSelectBK");
								}
							})
						})*/
						$.each(itemFligthListIs,function(i,n){
								itemFligthListHtml+=n.subName+",";
						})
						$("#specialAirport").val(specialSubString(itemFligthListHtml));
					}
					
				}
				
			},
			error:function(e){
				layer.msg(e)
			}
		})
		};
		//根据模板id来选择模板显示初始化
		var getTempId=window.location.search;
		if(getTempId.search("tempId")>0){
			specialTempShow(getTempId.substring(8,getTempId.indexOf("&")));
		}
});
function changePortSelect(obj){
	$(obj).toggleClass("airportSelectBK");
}
//获取字符串长度（汉字算两个字符，字母数字算一个）
function getByteLen(val) {
  var len = 0;
  for (var i = 0; i < val.length; i++) {
    var a = val.charAt(i);
    if (a.match(/[^\x00-\xff]/ig) != null) {
      len += 2;
    }
    else {
      len += 1;
    }
  }
  return len;
}